package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.common.utils.factory.DBConnectFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IMenuDao;
import com.hsbc.dev.teamo4.sms.sm.po.Department;
import com.hsbc.dev.teamo4.sms.sm.po.Menu;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.apache.xmlbeans.impl.jam.mutable.MElement;

import java.sql.SQLException;
import java.util.*;

/**
 * @author:Cady
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018-07-25
 */
public class MenuDaoImpl implements IMenuDao {

    /**
     * @Description save Menu
     * @Date: 15:30 2018-07-27
     * @Param menu
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu saveEntity(Menu menu) {
        String sql = "insert into sm_menu values(?,?,?,?,?,?,?,?,?,?,?)";
        return (Menu) DBUtil.saveEntity(sql,menu,null);
    }

    /**
     * @Description update menu
     * @Date: 15:31 2018-07-27
     * @Param menu
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu updateEntity(Menu menu) {
        String sql = "update sm_menu set menuName=?,menuHref=?,menuLevel=?,menuOrder=?,menuParentId=?,updateId=?,updateTime=?,isEnable=? where id=?";
        return (Menu) DBUtil.updateEntity(sql,menu, Lists.newArrayList("id","createId","createTime"),menu.getId());
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_menu set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    /**
     * @Description query menu by id
     * @Date: 15:31 2018-07-27
     * @Param id
     * @return com.hsbc.dev.teamo4.sms.sm.po.Menu
     */
    @Override
    public Menu queryById(String id) {
        String sql = "select * from sm_menu where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(Menu.class);
        return (Menu) DBUtil.queryById(sql,beanHandler);
    }

    /**
     * @Description get all Menu
     * @Date: 15:31 2018-07-27
     * @Param
     * @return java.util.List<com.hsbc.dev.teamo4.sms.sm.po.Menu>
     */
    @Override
    public List<Menu> queryAllEntity() {
        String sql = "select * from sm_menu where isEnable='"+true+"'";
        BeanListHandler beanListHandler=new BeanListHandler<>(Menu.class);
        return (List<Menu>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    /**
     * @Description set menu.isEnable to 0
     * @Date: 15:31 2018-07-27
     * @Param id
     * @return void
     */
    @Override
    public void deleteMenuById(String id){
        QueryRunner qr= DBConnectFactory.getQueryRunner();
        String sql = "update sm_menu set isEnable=? where id=?";
        Object params[] = { false, id };
        try {
            qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Menu> getChildMenuListByLevel(String level) {
        String sql="select * from sm_menu where menuLevel like '"+level+"' || '.%'";
        BeanListHandler beanListHandler=new BeanListHandler<>(Menu.class);
        return (List<Menu>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    @Override
    public void batchUpdateLevel(List<Menu> menus) {
        for (Menu  menu:menus){
            updateEntity(menu);
        }
    }

    @Override
    public Map countByNameAndParentId(String parentId, String menuName, String menuId) {
        String sql="select count(1) from sm_menu where menuParentId='"+parentId+"' and  menuName='"+menuName+"' and id= '"+menuId+"'";
        MapHandler mapHandler=new MapHandler();
        return (Map) DBUtil.countAndToMap(sql,mapHandler);
    }


}
